package com.zenkeeuk.bookerfornfc.client.android;

import java.util.List;

import com.zenkeeuk.bookerfornfc.client.android.book.BookReviewAdapter;
import com.zenkeeuk.bookerfornfc.client.android.book.DoubanBookInfo;
import com.zenkeeuk.bookerfornfc.client.android.history.HistoryManager;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;

public class HomeHistoryActivity extends ListActivity {
	
	List<DoubanBookInfo> listResilt;
	
	@Override
	protected void onCreate(Bundle icicle) {
	    super.onCreate(icicle);
	    setContentView(R.layout.homehistorylist);
	    
	    getHistoryList();
	}
	
    @Override
    public void onResume() {
        super.onResume();
        setContentView(R.layout.homehistorylist);
	    
	    getHistoryList();
    }
	
	@Override
	protected void onRestart(){
		super.onRestart();
		getHistoryList();
	}
	
	@Override
	protected void onListItemClick(ListView l, View view, int position, long id) {
		super.onListItemClick(l, view, position, id);
		
		DoubanBookInfo book =  listResilt.get(position);
  	  	Toast.makeText(getApplicationContext(), book.getBookName(), Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getBaseContext(), BookDetailActivity.class);
	    intent.putExtra(Contents.INTENT_EXTRA_ISBN_KEY, book.getBookisbn());
        startActivity(intent);
	}
	
	private void getHistoryList(){
	    listResilt = HistoryManager.getHistoryItems(Contents.NEED_BARCODEFORMAT, this.getApplicationContext());

	    ListView lv = getListView();
        
        BookReviewAdapter reviewAdapter;
        if (listResilt.size() > 0) {  
        	reviewAdapter = new BookReviewAdapter(this, listResilt);
        }
        else {
        	reviewAdapter = null;
        }
        
        setListAdapter(reviewAdapter);
        
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        	public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        		final DoubanBookInfo book =  listResilt.get(position);
        		
        		AlertDialog dialog = new AlertDialog.Builder(HomeHistoryActivity.this)
                .setTitle(getString(R.string.history_selector_title))
                .setItems(getResources().getStringArray(R.array.book_operation_choices), 
                	new OnClickListener() {
                	public void onClick(DialogInterface dialog, int which) {  
            	        switch (which) {  
            	        	case 0:
            	        		HistoryManager.deleteBookInHistory(book.getBookisbn(), getBaseContext());
            	        		listResilt.remove(position);
            	        		BookReviewAdapter reviewAdapter = new BookReviewAdapter(HomeHistoryActivity.this, listResilt);
            	                setListAdapter(reviewAdapter);
            	        		break;
            	        	case 1:
            	        		
            	        		break;
            	        }
            	    }
            	}).create();
        		dialog.show();
        		
        		
        		return true;
        	}
        });
	}
	
}
